#!/bin/bash
links_sorted_file="links-sorted.tmp"
links_unsorted_file="links-unsorted.tmp"
linkname_list_file="actions-linkname-list_test-final.txt"

add_alternative_names_to_file() {
	name=${1}
	file=${2}
	echo -n " ${name} ${name}-symbolic" >>${file}
	if test -n $(echo ${name} | grep '\-rtl') ; then
		echo -n " ${name/-rtl/-symbolic-rtl}" >>${file}
	fi
	if test -n $(echo ${name} | grep '\-ltr') ; then
		echo -n " ${name/-ltr/-symbolic-ltr}" >>${file}
	fi
}

rm -v ${links_unsorted_file}
for l in $(find ./scalable--backup -type l) ; do
	file=$(basename $(realpath -e ${l})) 
	file=${file/.24x24.svg/}
	link=$(basename ${l})
	link=${link/.svg/}
	if test -z $(echo ${link} | grep '\-symbolic') ; then
		echo "${file} ${link}" >>${links_unsorted_file}
	fi
done

rm -v ${linkname_list_file}
cat ${links_unsorted_file} | sort >${links_sorted_file}

rm -v ${linkname_list_file}
for f in $(find ../.images/actions/*.24x24.svg -type f) ; do
	name=$(basename ${f/.24x24.svg/})
	echo "WORKING_ON: NAME=${name}"
	n=${name}
	add_alternative_names_to_file ${n} ${linkname_list_file}
	echo -n "+"
	grep -v '^#.*$\|^[[:space:]]*$' ${links_sorted_file} |\
	while IFS='' read -r line || test -n "${line}" ; do
		a=$(echo ${line} | awk '{print $1}')
		if test "${a}" = "${name}" ; then
			n=$(echo ${line} | awk '{print $2}')
			if test "${n}" != "${name}" ; then
				add_alternative_names_to_file ${n} ${linkname_list_file}
				echo -n "+"
			fi
		fi
	done
	echo "" >>${linkname_list_file}
	echo ""
	echo "DONE: ${f}"
done

